/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view;

/**
 *
 * @author Laurent WOUTERS
 */
public class WinOutput extends javax.swing.JPanel {
    public static interface Document {
        public void write(String message);
        public void writeLine(String message);
        public void write(String message, java.awt.Color color);
        public void writeLine(String message, java.awt.Color color);
    }
    
    private static abstract class DocumentImpl implements Document {
        private javax.swing.JScrollPane content;
        private javax.swing.JPanel head;
        private javax.swing.JTextPane area;
        
        public javax.swing.JComponent getTabContent() { return content; }
        public javax.swing.JComponent getTabHead() { return head; }
        
        protected abstract void onClose(javax.swing.JScrollPane tab);
        
        public DocumentImpl(String title, ViewStyle style) {
            javax.swing.JLabel label = new javax.swing.JLabel(title);
            label.setOpaque(false);
            javax.swing.JLabel close = new javax.swing.JLabel(IconProvider.getEmbedded("close.png"));
            close.setMinimumSize(new java.awt.Dimension(17, 17));
            close.setPreferredSize(new java.awt.Dimension(17, 17));
            close.setMaximumSize(new java.awt.Dimension(17, 17));
            head = new javax.swing.JPanel();
            head.setOpaque(false);
            head.setLayout(new javax.swing.BoxLayout(head, javax.swing.BoxLayout.X_AXIS));
            head.add(label);
            head.add(javax.swing.Box.createHorizontalStrut(5));
            head.add(close);
            area = new javax.swing.JTextPane();
            area.setFont(new java.awt.Font(style.outputFontName, style.outputFontStyle, style.outputFontSize));
            content = new javax.swing.JScrollPane(area);
            close.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override public void mouseReleased(java.awt.event.MouseEvent me) { onClose(content); }
            });
        }
        
       @Override public void write(String message) {
            try {
                area.getStyledDocument().insertString(area.getStyledDocument().getLength(), message, null);
            } catch (javax.swing.text.BadLocationException ex) { }
        }
        @Override public void writeLine(String message) {
            try {
                area.getStyledDocument().insertString(area.getStyledDocument().getLength(), message + java.lang.System.lineSeparator(), null);
            } catch (javax.swing.text.BadLocationException ex) { }
        }
        @Override public void write(String message, java.awt.Color color) {
            try {
                javax.swing.text.SimpleAttributeSet attributes = new javax.swing.text.SimpleAttributeSet();
                javax.swing.text.StyleConstants.setForeground(attributes, color);
                area.getStyledDocument().insertString(area.getStyledDocument().getLength(), message, attributes);
            } catch (javax.swing.text.BadLocationException ex) { }
        }
        @Override public void writeLine(String message, java.awt.Color color) {
            try {
                javax.swing.text.SimpleAttributeSet attributes = new javax.swing.text.SimpleAttributeSet();
                javax.swing.text.StyleConstants.setForeground(attributes, color);
                area.getStyledDocument().insertString(area.getStyledDocument().getLength(), message + java.lang.System.lineSeparator(), attributes);
            } catch (javax.swing.text.BadLocationException ex) { }
        }
    }
    
    private ViewStyle style;
    private javax.swing.JTabbedPane tabs;
    
    public WinOutput(ViewStyle style) {
        this.style = style;
        this.tabs = new javax.swing.JTabbedPane();
        this.setLayout(new java.awt.BorderLayout());
        this.add(tabs, java.awt.BorderLayout.CENTER);
    }
    
    public Document newDocument(String title) {
        DocumentImpl impl = new DocumentImpl(title, style) {
            @Override protected void onClose(javax.swing.JScrollPane tab) { tabs.remove(tab); }
        };
        tabs.add(impl.getTabContent());
        tabs.setTabComponentAt(tabs.getTabCount()-1, impl.getTabHead());
        tabs.setToolTipTextAt(tabs.getTabCount()-1, title);
        tabs.setSelectedIndex(tabs.getTabCount()-1);
        this.revalidate();
        this.repaint();
        return impl;
    }
}
